package module13;

import java.util.List;

import org.apache.http.client.fluent.Request;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class ExtractAllLinks {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		WebDriver driver = new FirefoxDriver();
		driver.get("http://www.bbc.com");
		driver.manage().window().maximize();
		
		WebElement box = driver.findElement(By.tagName("a"));
        List<WebElement> links= driver.findElements(By.tagName("a"));
		System.out.println(links.size());
		for(int i=5;i<links.size();i++)
		{
			try
			{
			System.out.println(links.get(i).getText());
			
			//String url = driver.getCurrentUrl();
			String url = links.get(i).getAttribute("href");
			int response_code = Request.Get("url").execute().returnResponse().getStatusLine().getStatusCode();
			System.out.println("Response code for url" +url+ "is:" +response_code);
			if(response_code == 200)
			{
				System.out.println("Pass");
				links.get(i).click();
				Thread.sleep(3000);
			}
			else
			{
				System.out.println("Fail");

			}
			System.out.println(driver.getTitle());
			driver.get("http://www.bbc.com");
			System.out.println("*****************************");
		    box = driver.findElement(By.tagName("a"));
		    links = driver.findElements(By.tagName("a"));
			}
			catch(Throwable t)
			{
				System.out.println("Error happened");
			}
		}

	}

}
